﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.Enum;

public partial class Pages_CallCategoryUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Session["id"].ToString();
            CallCategory callCategory = new CallCategory();
            callCategory.Id = Convert.ToInt32(id);
            CallCategoryController Controller = new CallCategoryController();
            CallCategory returnedCallCategory = new CallCategory();
            try
            {
                returnedCallCategory = Controller.GetCallCategory(callCategory);
                if (returnedCallCategory != null)
                {
                    txbDescription.Text = returnedCallCategory.Description;
                    txbMinimumTimeAnalize.Text = Convert.ToString(returnedCallCategory.MinimumTimeAnalize);
                    txbMaximumTimeAnalize.Text = Convert.ToString(returnedCallCategory.MaximumTimeAnalize);
                    txbMaximumTimeTreatment.Text = Convert.ToString(returnedCallCategory.MaximumTimeTreatment);
                    txbMinimumTimeTreatment.Text = Convert.ToString(returnedCallCategory.MinimumTimeTreatment);
                }
            }
            catch (Exception ex)
            {
                Response.Write(@"<script language='javascript'>alert('Ocorreu um erro no carregamento da tela: <br>" + ex.Message +
                                   ".');</script>");
            }
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        CallCategory callCategory = new CallCategory();
        CallCategoryController Controller = new CallCategoryController();
        try
        {
            if (Controller.UpdateCallCategory(populateObject(callCategory)) == EnumGenericState.GENERIC_STATE_SUCCESS)
            {
                Response.Write(@"<script language='javascript'>alert('Estado Corrigido com Sucesso!');</script>");
                ClearFields();
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('Ocorreu um erro durante a correção, por favor tente novamente!');</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro na correção: <br>" + ex.Message +
                               ".');</script>");
            callCategory = null;
        }
        finally
        {
            Response.Redirect("CallCategoryMain.aspx");
            Session.Remove("id");
        }
    }

    public void ClearFields()
    {
        txbDescription.Text = "";
        txbMaximumTimeAnalize.Text = "";
        txbMaximumTimeTreatment.Text = "";
        txbMinimumTimeAnalize.Text = "";
        txbMinimumTimeTreatment.Text = "";
        txbDescription.Focus();
    }

    public CallCategory populateObject(CallCategory callCategory)
    {
        string id = Session["id"].ToString();
        callCategory.Id = Convert.ToInt32(id);
        callCategory.Description = txbDescription.Text;
        callCategory.MinimumTimeAnalize = Convert.ToDouble(txbMinimumTimeAnalize.Text);
        callCategory.MaximumTimeAnalize = Convert.ToDouble(txbMaximumTimeAnalize.Text);
        callCategory.MaximumTimeTreatment = Convert.ToDouble(txbMaximumTimeTreatment.Text);
        callCategory.MinimumTimeTreatment = Convert.ToDouble(txbMinimumTimeTreatment.Text);
        return callCategory;
    }

}